#define  _CRT_SECURE_NO_WARNINGS

#include "ObjectPool.h"
#include "ConcurrentAlloc.h"

//void TestTLS()
//{
//	std::thread t1(Alloc1);
//	t1.join();
//
//	std::thread t2(Alloc2);
//	t2.join();
//}

void TestConcurrentPool1()
{
	void* p1 = ConcurrentAlloc(1);
	void* p2 = ConcurrentAlloc(7);
	void* p3 = ConcurrentAlloc(8);
	void* p4 = ConcurrentAlloc(6);
	void* p5 = ConcurrentAlloc(16);

	std::cout << p1 << std::endl;
	std::cout << p2 << std::endl;
	std::cout << p3 << std::endl;
	std::cout << p4 << std::endl;
	std::cout << p5 << std::endl;

	ConcurrentFree(p1);
	ConcurrentFree(p2);
	ConcurrentFree(p3);
	ConcurrentFree(p4);
	ConcurrentFree(p5);
}

void TestConcurrentPool2()
{
	for (int i = 0; i < 1024; i++)
	{
		void* p = ConcurrentAlloc(5);
		std::cout << p << std::endl;
	}

	void* p = ConcurrentAlloc(6);
	std::cout << p << std::endl;
}

void Alloc1()
{
	std::vector<void*> v;

	for (int i = 0; i < 5; i++)
	{
		void* p = ConcurrentAlloc(6);
		std::cout << p << std::endl;
		v.push_back(p);
	}

	for (int i = 0; i < 5; i++)
		ConcurrentFree(v[i]);
}

void Alloc2()
{
	std::vector<void*> v;

	for (int i = 0; i < 5; i++)
	{
		void* p = ConcurrentAlloc(16);
		std::cout << p << std::endl;
		v.push_back(p);
	}

	for (int i = 0; i < 5; i++)
		ConcurrentFree(v[i]);
}

void TestBigMemory()
{
	void* p1 = ConcurrentAlloc(33 * 8 * 1024);
	std::cout << p1 << std::endl;
	ConcurrentFree(p1);

	void* p2 = ConcurrentAlloc(129 * 8 * 1024);
	std::cout << p2 << std::endl;
	ConcurrentFree(p2);
}

void MultiThreadAlloc1()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; ++i)
	{
		void* ptr = ConcurrentAlloc(6);
		v.push_back(ptr);
	}

	for (size_t i = 0; i < 7; ++i)
	{
		ConcurrentFree(v[i]);
	}
}

void MultiThreadAlloc2()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; ++i)
	{
		void* ptr = ConcurrentAlloc(16);
		v.push_back(ptr);
	}

	for (size_t i = 0; i < 7; ++i)
		ConcurrentFree(v[i]);
}

void TestThreadAlloc1()
{
	//std::thread t1(Alloc1);
	//std::thread t2(Alloc2);

	//t1.join();
	//t2.join();

	std::thread t1(MultiThreadAlloc1);
	std::thread t2(MultiThreadAlloc2);

	t1.join();
	t2.join();
}

//int main()
//{
//	//TestObjectPool();
//	//TestTLS();
//	//TestConcurrentPool2();
//	//TestConcurrentPool1();
//	TestBigMemory();
//	TestThreadAlloc1();
//	return 0;
//}